/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package gokyesil.game.aygitciklar;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.ModelLoader;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.lights.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.lights.Lights;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;

/**
 * See: http://blog.xoppa.com/loading-models-using-libgdx/
 * @author Xoppa
 */
public class LoadModelsTest implements ApplicationListener {
    public PerspectiveCamera cam;
    public CameraInputController camController;
    public ModelBatch modelBatch;
    private Model ship;
    public AssetManager assets;
    public Array<ModelInstance> instances = new Array<ModelInstance>();
    public Lights lights;
    public boolean loading;
	private float z;
	private float y;
	private float x;
	private float angle;
	private ModelInstance ins;
	private static final float CAMERA_WIDTH = 25.0f;
	private static final float CAMERA_HEIGHT = 15.0f;
	private static final float ASPECT_RATIO = 1.66667f;
     
    @Override
    public void create () {
        modelBatch = new ModelBatch();
        lights = new Lights();
        lights.ambientLight.set(40f, 40f, 40f, 100f);
        lights.add(new DirectionalLight().set(10f, 10f, 10f, 0f, 0f, 0f)); 
        cam = new PerspectiveCamera(30, CAMERA_WIDTH, CAMERA_HEIGHT);
        cam.position.set(0f, 0f, 28.1f);
        cam.lookAt(0,0,0);
        cam.near = 0.1f;
        cam.far = 300f;
        cam.update();
        
 
       // camController = new CameraInputController(cam);
       // Gdx.input.setInputProcessor(camController);
         
       // assets = new AssetManager();
      //  assets.load("assets/cube.obj", Model.class);
      //  loading = true;
        
        ModelLoader loader = new ObjLoader();
        ship = loader.loadModel(Gdx.files.internal("assets/cube.obj"));
        x=0;
        y=0;
        z=0;
        angle = 0;
        ins = new ModelInstance(ship,x,y,z);
      //  ins.transform.translate(0.0f, 0.0f, 0.0f).rotate(new Vector3(1,0,0), 90);
        
    }
 
    private void doneLoading() {
        Model ship = assets.get("assets/cube.obj", Model.class);
        ModelInstance shipInstance = new ModelInstance(ship); 
        instances.add(shipInstance);
        loading = false;
    }
     
    @Override
    public void render () {
        /*if (loading && assets.update())
            doneLoading();
        camController.update();*/
         
        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
 
        modelBatch.begin(cam);
       
       // ins.transform.translate(0.001f, 0.001f, 0.001f);
      //  x += 0.000001;
      //  y += 0.000001;
      //  z += 0.000001;
        
        modelBatch.render(ins,lights);
      //  angle += 0.0001f;
        
        
       /*for (ModelInstance instance : instances)
            modelBatch.render(instance, lights);*/
       
        modelBatch.end();
    }
    
    @Override
    public void dispose () {
        modelBatch.dispose();
        instances.clear();
        assets.dispose();
    }
 
    public void resume () {
    }
 
    public void resize (int width, int height) 
    {
    	
    	//cam.viewportWidth = width;
		//cam.viewportHeight = height;
		//cam.update();
		/*
		float ratio = (float)width / (float)height;
		Vector2 trans = new Vector2();
		if(ratio < ASPECT_RATIO)
		{
			height = (int)(CAMERA_HEIGHT*ASPECT_RATIO/ratio);
			width =  (int) CAMERA_WIDTH;
			trans.set(0,height-CAMERA_HEIGHT);
		}
		else
		{
			width = (int)(CAMERA_WIDTH*ratio/ASPECT_RATIO);
			height =  (int) CAMERA_HEIGHT;
			trans.set(width-CAMERA_WIDTH,0);
		}
		
		cam.viewportWidth = width;
		cam.viewportHeight = height;
		cam.position.set((width- trans.x)/2.0f, (height-trans.y)/2.0f,1);
		*/
    }
 
    public void pause () {
    }
}